package managedbean;

import entity.BranchAddressEntity;
import entity.OperatingHoursEntity;
import entity.CuisineTypeEntity;
import entity.ExtendedOperatingHoursEntity;
import entity.FnBBranchEntity;
import entity.OperatingHoursRemarksEntity;
import entity.StakeholderEntity;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.PhaseEvent;
import session.stateless.BranchSessionBean;
import session.stateless.SystemMessagesLocal;
import util.enumeration.CuisineType;
import util.enumeration.OperatingDay;
import util.enumeration.OperatingHours;
import util.enumhelper.OperatingHrs;
import util.localization.WebLocalization;

/**
 *
 * @author Amelia
 */
@ManagedBean
@ViewScoped
public class EditBranchManagedBean implements Serializable {
    //Exception

    @EJB
    private SystemMessagesLocal systemMessages;
    //User Selected Locale
    private String preferredLocale;
    private WebLocalization localization;
    @EJB
    private BranchSessionBean branchSessionBean;
    private StakeholderEntity stakeholder;
    private BranchAddressEntity branchAddressToEdit;
    private FnBBranchEntity fnbBranchToEdit;
    private OperatingHoursRemarksEntity operatingHoursRemarksToEdit;
    private List<CuisineTypeEntity> cuisinePrefToEdit;
    private List<OperatingHrs> extendedOperatingHoursList;
    private List<OperatingHoursEntity> branchOperatingHoursToEdit;
    List<ExtendedOperatingHoursEntity> extOpHrsList;
    private List<CuisineType> cuisineTypeList;
    private List<OperatingHrs> operatingHrsList;
    private OperatingDay[] operatingDay = OperatingDay.values();
    private OperatingHours mondayStart;
    private OperatingHours mondayEnd;
    private OperatingHours tuesdayStart;
    private OperatingHours tuesdayEnd;
    private OperatingHours wednesdayStart;
    private OperatingHours wednesdayEnd;
    private OperatingHours thursdayStart;
    private OperatingHours thursdayEnd;
    private OperatingHours fridayStart;
    private OperatingHours fridayEnd;
    private OperatingHours saturdayStart;
    private OperatingHours saturdayEnd;
    private OperatingHours sundayStart;
    private OperatingHours sundayEnd;
    private OperatingHours extendedTimeStart;
    private OperatingHours extendedTimeEnd;
    private Date extendedCalendarDate;
    private boolean branchEdited;

    /** Creates a new instance of EditBranchManagedBean */
    public EditBranchManagedBean() {
    }

    /************************************************************************************\
    /******************************** GET SET METHODS ***********************************\
    /************************************************************************************/
    public BranchAddressEntity getBranchAddressToEdit() {
        return branchAddressToEdit;
    }

    public void setBranchAddressToEdit(BranchAddressEntity branchAddressToEdit) {
        this.branchAddressToEdit = branchAddressToEdit;
    }

    public FnBBranchEntity getFnbBranchToEdit() {
        return fnbBranchToEdit;
    }

    public void setFnbBranchToEdit(FnBBranchEntity fnbBranchToEdit) {
        this.fnbBranchToEdit = fnbBranchToEdit;
    }

    public OperatingHoursRemarksEntity getOperatingHoursRemarksToEdit() {
        return operatingHoursRemarksToEdit;
    }

    public void setOperatingHoursRemarksToEdit(OperatingHoursRemarksEntity operatingHoursRemarksToEdit) {
        this.operatingHoursRemarksToEdit = operatingHoursRemarksToEdit;
    }

    public List<CuisineTypeEntity> getCuisinePrefToEdit() {
        return cuisinePrefToEdit;
    }

    public void setCuisinePrefToEdit(List<CuisineTypeEntity> cuisinePrefToEdit) {
        this.cuisinePrefToEdit = cuisinePrefToEdit;
    }

    public List<OperatingHoursEntity> getBranchOperatingHoursToEdit() {
        return branchOperatingHoursToEdit;
    }

    public void setBranchOperatingHoursToEdit(List<OperatingHoursEntity> branchOperatingHoursToEdit) {
        this.branchOperatingHoursToEdit = branchOperatingHoursToEdit;
    }

    public List<OperatingHrs> getExtendedOperatingHoursList() {
        return extendedOperatingHoursList;
    }

    public void setExtendedOperatingHoursList(List<OperatingHrs> extendedOperatingHoursList) {
        this.extendedOperatingHoursList = extendedOperatingHoursList;
    }

    public List<CuisineType> getCuisineTypeList() {
        return cuisineTypeList;
    }

    public void setCuisineTypeList(List<CuisineType> cuisineTypeList) {
        this.cuisineTypeList = cuisineTypeList;
    }

    public List<OperatingHrs> getOperatingHrsList() {
        return operatingHrsList;
    }

    public void setOperatingHrsList(List<OperatingHrs> operatingHrsList) {
        this.operatingHrsList = operatingHrsList;
    }

    public OperatingHours getMondayStart() {
        return mondayStart;
    }

    public void setMondayStart(OperatingHours mondayStart) {
        this.mondayStart = mondayStart;
    }

    public OperatingHours getMondayEnd() {
        return mondayEnd;
    }

    public void setMondayEnd(OperatingHours mondayEnd) {
        this.mondayEnd = mondayEnd;
    }

    public OperatingHours getTuesdayStart() {
        return tuesdayStart;
    }

    public void setTuesdayStart(OperatingHours tuesdayStart) {
        this.tuesdayStart = tuesdayStart;
    }

    public OperatingHours getTuesdayEnd() {
        return tuesdayEnd;
    }

    public void setTuesdayEnd(OperatingHours tuesdayEnd) {
        this.tuesdayEnd = tuesdayEnd;
    }

    public OperatingHours getWednesdayStart() {
        return wednesdayStart;
    }

    public void setWednesdayStart(OperatingHours wednesdayStart) {
        this.wednesdayStart = wednesdayStart;
    }

    public OperatingHours getWednesdayEnd() {
        return wednesdayEnd;
    }

    public void setWednesdayEnd(OperatingHours wednesdayEnd) {
        this.wednesdayEnd = wednesdayEnd;
    }

    public OperatingHours getThursdayStart() {
        return thursdayStart;
    }

    public void setThursdayStart(OperatingHours thursdayStart) {
        this.thursdayStart = thursdayStart;
    }

    public OperatingHours getThursdayEnd() {
        return thursdayEnd;
    }

    public void setThursdayEnd(OperatingHours thursdayEnd) {
        this.thursdayEnd = thursdayEnd;
    }

    public OperatingHours getFridayStart() {
        return fridayStart;
    }

    public void setFridayStart(OperatingHours fridayStart) {
        this.fridayStart = fridayStart;
    }

    public OperatingHours getFridayEnd() {
        return fridayEnd;
    }

    public void setFridayEnd(OperatingHours fridayEnd) {
        this.fridayEnd = fridayEnd;
    }

    public OperatingHours getSaturdayStart() {
        return saturdayStart;
    }

    public void setSaturdayStart(OperatingHours saturdayStart) {
        this.saturdayStart = saturdayStart;
    }

    public OperatingHours getSaturdayEnd() {
        return saturdayEnd;
    }

    public void setSaturdayEnd(OperatingHours saturdayEnd) {
        this.saturdayEnd = saturdayEnd;
    }

    public OperatingHours getSundayStart() {
        return sundayStart;
    }

    public void setSundayStart(OperatingHours sundayStart) {
        this.sundayStart = sundayStart;
    }

    public OperatingHours getSundayEnd() {
        return sundayEnd;
    }

    public void setSundayEnd(OperatingHours sundayEnd) {
        this.sundayEnd = sundayEnd;
    }

    public OperatingHours getExtendedTimeStart() {
        return extendedTimeStart;
    }

    public void setExtendedTimeStart(OperatingHours extendedTimeStart) {
        this.extendedTimeStart = extendedTimeStart;
    }

    public OperatingHours getExtendedTimeEnd() {
        return extendedTimeEnd;
    }

    public void setExtendedTimeEnd(OperatingHours extendedTimeEnd) {
        this.extendedTimeEnd = extendedTimeEnd;
    }

    public Date getExtendedCalendarDate() {
        return extendedCalendarDate;
    }

    public void setExtendedCalendarDate(Date extendedCalendarDate) {
        this.extendedCalendarDate = extendedCalendarDate;
    }

    /************************************************************************************\
    /*********************************** METHODS ****************************************\
    /************************************************************************************/
    public void initView(PhaseEvent event) {
        /* LOCALIZATION */
        localization = new WebLocalization();
        preferredLocale = localization.getPreferredLocaleFromSession();
        /* END LOCALIZATION */
        FacesContext.getCurrentInstance().getExternalContext().getSession(true);
        /*
        if (stakeholder == null) {
        stakeholder = (StakeholderEntity) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("stakeholderEntity");
        }
        if (branchToEdit == null) {
        branchToEdit = (FnbBranchState) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("selectedBranch");
        }*/
        if (fnbBranchToEdit == null) {
            fnbBranchToEdit = (FnBBranchEntity) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("selectedFnBBranch");
        }
        if (branchAddressToEdit == null) {
            branchAddressToEdit = (BranchAddressEntity) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("selectedBranchAddress");
        }
        if (cuisinePrefToEdit == null) {
            this.cuisineTypeList = new ArrayList<CuisineType>();
            cuisinePrefToEdit = (List<CuisineTypeEntity>) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("selectedCuisinePref");
            //Database records will be placed into the cuisineType enum arraylist which is mapped onto editBranch.xhtml
            for (int i = 0; i < getCuisinePrefToEdit().size(); i++) {
                //arraylist add the values
                cuisineTypeList.add(getCuisinePrefToEdit().get(i).getCuisineTypeEnum());
            }
        }
        if (branchOperatingHoursToEdit == null) {
            branchOperatingHoursToEdit = (List<OperatingHoursEntity>) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("selectedBranchOperatingHours");
            //Database records will be placed into the individual days record which is mapped onto editBranch.xhtml
            setMondayStart(getBranchOperatingHoursToEdit().get(0).getOpeningHoursEnum());
            setMondayEnd(getBranchOperatingHoursToEdit().get(0).getClosingHoursEnum());
            setTuesdayStart(getBranchOperatingHoursToEdit().get(1).getOpeningHoursEnum());
            setTuesdayEnd(getBranchOperatingHoursToEdit().get(1).getClosingHoursEnum());
            setWednesdayStart(getBranchOperatingHoursToEdit().get(2).getOpeningHoursEnum());
            setWednesdayEnd(getBranchOperatingHoursToEdit().get(2).getClosingHoursEnum());
            setThursdayStart(getBranchOperatingHoursToEdit().get(3).getOpeningHoursEnum());
            setThursdayEnd(getBranchOperatingHoursToEdit().get(3).getClosingHoursEnum());
            setFridayStart(getBranchOperatingHoursToEdit().get(4).getOpeningHoursEnum());
            setFridayEnd(getBranchOperatingHoursToEdit().get(4).getClosingHoursEnum());
            setSaturdayStart(getBranchOperatingHoursToEdit().get(5).getOpeningHoursEnum());
            setSaturdayEnd(getBranchOperatingHoursToEdit().get(5).getClosingHoursEnum());
            setSundayStart(getBranchOperatingHoursToEdit().get(6).getOpeningHoursEnum());
            setSundayEnd(getBranchOperatingHoursToEdit().get(6).getClosingHoursEnum());
        }
        if (extOpHrsList == null) {
            this.extendedOperatingHoursList = new ArrayList<OperatingHrs>();
            this.extOpHrsList = new ArrayList<ExtendedOperatingHoursEntity>();
            extOpHrsList = (List<ExtendedOperatingHoursEntity>) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("selectedExtendedOperatingHours");
            for (int i = 0; i < extOpHrsList.size(); i++) {
                OperatingHrs opHrClass = new OperatingHrs(extOpHrsList.get(i).getOperatingDayEnum(), extOpHrsList.get(i).getOperatingDay(), extOpHrsList.get(i).getOpeningHoursEnum(), extOpHrsList.get(i).getOpeningHours(), extOpHrsList.get(i).getClosingHoursEnum(), extOpHrsList.get(i).getClosingHours(), extOpHrsList.get(i).getSpecificDate());
                extendedOperatingHoursList.add(opHrClass);
            }
        }
        if (operatingHoursRemarksToEdit == null) {
            OperatingHoursRemarksEntity operatingHoursRemarks = branchSessionBean.viewOperatingHoursRemarks(preferredLocale, fnbBranchToEdit.getId());
            operatingHoursRemarksToEdit = operatingHoursRemarks;
        }

    }

    public void editBranch(ActionEvent event) {
        this.operatingHrsList = new ArrayList<OperatingHrs>();

        OperatingHrs monOpHrs = new OperatingHrs(operatingDay[0], operatingDay[0].getLabel(), getMondayStart(), getMondayStart().getTime(), getMondayEnd(), getMondayEnd().getTime());
        OperatingHrs tuesOpHrs = new OperatingHrs(operatingDay[1], operatingDay[1].getLabel(), getTuesdayStart(), getTuesdayStart().getTime(), getTuesdayEnd(), getTuesdayEnd().getTime());
        OperatingHrs wedOpHrs = new OperatingHrs(operatingDay[2], operatingDay[2].getLabel(), getWednesdayStart(), getWednesdayStart().getTime(), getWednesdayEnd(), getWednesdayEnd().getTime());
        OperatingHrs thursOpHrs = new OperatingHrs(operatingDay[3], operatingDay[3].getLabel(), getThursdayStart(), getThursdayStart().getTime(), getThursdayEnd(), getThursdayEnd().getTime());
        OperatingHrs friOpHrs = new OperatingHrs(operatingDay[4], operatingDay[4].getLabel(), getFridayStart(), getFridayStart().getTime(), getFridayEnd(), getFridayEnd().getTime());
        OperatingHrs satOpHrs = new OperatingHrs(operatingDay[5], operatingDay[5].getLabel(), getSaturdayStart(), getSaturdayStart().getTime(), getSaturdayEnd(), getSaturdayEnd().getTime());
        OperatingHrs sunOpHrs = new OperatingHrs(operatingDay[6], operatingDay[6].getLabel(), getSundayStart(), getSundayStart().getTime(), getSundayEnd(), getSundayEnd().getTime());
        getOperatingHrsList().add(monOpHrs);
        getOperatingHrsList().add(tuesOpHrs);
        getOperatingHrsList().add(wedOpHrs);
        getOperatingHrsList().add(thursOpHrs);
        getOperatingHrsList().add(friOpHrs);
        getOperatingHrsList().add(satOpHrs);
        getOperatingHrsList().add(sunOpHrs);

        branchEdited = branchSessionBean.editBranchDetails(getFnbBranchToEdit(), getBranchOperatingHoursToEdit(),
                getBranchAddressToEdit(), getFnbBranchToEdit().getBranchName(), getFnbBranchToEdit().getBranchAddress().getPostalCode(),
                getFnbBranchToEdit().getBranchAddress().getHouseNumber(), getFnbBranchToEdit().getBranchAddress().getStreetName(),
                getFnbBranchToEdit().getBranchAddress().getUnitNumber(), getFnbBranchToEdit().getBranchAddress().getBuildingName(), getFnbBranchToEdit().getContactNumber(),
                getFnbBranchToEdit().getContactPerson(), getFnbBranchToEdit().getEmailAddress(), getFnbBranchToEdit().getFnBTypeEnum(),
                getFnbBranchToEdit().getIsHalalEnum(), getFnbBranchToEdit().getPriceRangeEnum(), preferredLocale, operatingHoursRemarksToEdit.getRemarks(), cuisineTypeList, operatingHrsList, extendedOperatingHoursList);

        if (branchEdited == true) {
            String statusMessage = systemMessages.getSystemMessage("BRANCH_EDIT_SUCCESS", preferredLocale);
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, statusMessage, statusMessage));
        } else {
            String statusMessage = systemMessages.getSystemMessage("BRANCH_EDIT_NOT_SUCCESSFUL", preferredLocale);
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, statusMessage, statusMessage));
        }
    }

    //Add a specific date list for extended operating hours
    public void addSpecificDateList() {
        OperatingHrs specificOperatingHrs = new OperatingHrs();
        boolean duplicate = false;

        //Check calendar date is empty or not
        if (extendedCalendarDate != null) {
            Date todayDate = new Date();
            //entered calendar date has to be after today's date
            if (extendedCalendarDate.before(todayDate) && !(extendedCalendarDate.equals(todayDate))) {
                String statusMessage = systemMessages.getSystemMessage("EXTENDED_DATE_NOT_BEFORE_TODAY'S_DATE", preferredLocale);
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, statusMessage, statusMessage));
            } else {
                //check whether the time entered is equal except for off
                if (extendedTimeStart.getTime().equals(extendedTimeEnd.getTime())
                        && !(extendedTimeStart.getLabel().equals("OFF") && extendedTimeEnd.getLabel().equals("OFF"))) {
                    String statusMessage = systemMessages.getSystemMessage("EXTENDED_START_TIME_END_TIME_MUST_BE_DIFFERENT", preferredLocale);
                    FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, statusMessage, statusMessage));
                } /*else if (extendedTimeStart.getTime().after(extendedTimeEnd.getTime())
                && (!(extendedTimeStart.getLabel().equals("OFF") && !(extendedTimeEnd.getLabel().equals("OFF"))))) {
                //String statusMessage = systemMessages.getSystemMessage("CREATION_SUCCESS", selectedLocale);
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Extended Operating Hours: Select an appropriate time range", "Extended Operating Hours: Select an appropriate time range"));
                } */ //check if 1 time is entered off, the other must also be off
                else if ((extendedTimeStart.getLabel().equals("OFF") && !(extendedTimeEnd.getLabel().equals("OFF")))
                        || (!(extendedTimeStart.getLabel().equals("OFF")) && extendedTimeEnd.getLabel().equals("OFF"))) {
                    String statusMessage = systemMessages.getSystemMessage("EXTENDED_START_TIME_END_TIME_MUST_BE_OFF", preferredLocale);
                    FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, statusMessage, statusMessage));
                } // if all goes well....
                else {
                    //Select the day string value from enum(Compare calendar day(int value) to enum day(int value))
                    for (int i = 0; i < operatingDay.length; i++) {
                        if (extendedCalendarDate.getDay() == operatingDay[i].getDay()) {
                            String day = operatingDay[i].getLabel();
                            specificOperatingHrs = new OperatingHrs(operatingDay[i], day, extendedTimeStart, extendedTimeStart.getTime(), extendedTimeEnd, extendedTimeEnd.getTime(), extendedCalendarDate);
                        }
                    }
                    //If there is no record exist in the list, add to arraylist
                    if (extendedOperatingHoursList.isEmpty()) {
                        extendedOperatingHoursList.add(specificOperatingHrs);
                        String statusMessage = systemMessages.getSystemMessage("ADDED_TO_LIST", preferredLocale);
                        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, statusMessage, statusMessage));
                    } else {
                        //traverse array list to search for duplicate record
                        //if exists, set msg and break from loop
                        for (int i = 0; i < extendedOperatingHoursList.size(); i++) {
                            //Check both dates is equal
                            if (extendedOperatingHoursList.get(i).getSpecificDate().equals(specificOperatingHrs.getSpecificDate())
                                    && ( //check new opening time has to be after closing time
                                    (extendedOperatingHoursList.get(i).getClosingHours().after(specificOperatingHrs.getOpeningHours()))
                                    || //check both opening n closing is equal
                                    (extendedOperatingHoursList.get(i).getOpeningHours().equals(specificOperatingHrs.getOpeningHours())
                                    && extendedOperatingHoursList.get(i).getClosingHours().equals(specificOperatingHrs.getClosingHours())))) {
                                //if all true, return duplicate msg
                                String statusMessage = systemMessages.getSystemMessage("DUPLICATE_DATE", preferredLocale);
                                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, statusMessage, statusMessage));
                                duplicate = true;
                                break;
                            }
                        }
                        //if there's no duplicate records in arraylist, add new record into list
                        if (duplicate == false) {
                            extendedOperatingHoursList.add(specificOperatingHrs);
                            String statusMessage = systemMessages.getSystemMessage("ADDED_TO_LIST", preferredLocale);
                            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, statusMessage, statusMessage));
                        }
                    }
                }
            }
        } else {
            String statusMessage = systemMessages.getSystemMessage("EXTENDED_IS_NOT_SELECTED", preferredLocale);
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, statusMessage, statusMessage));
        }
    }

    public void removeSpecificDateList(OperatingHrs extendedOperatingHrs) {
        extendedOperatingHoursList.remove(extendedOperatingHrs);
        String statusMessage = systemMessages.getSystemMessage("REMOVED_FROM_LIST", preferredLocale);
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, statusMessage, statusMessage));
    }

    /************************************************************************************|
    |*********************************** VALIDATION METHODS *****************************|
    |************************************************************************************/
    public void validateOperatingHours(FacesContext context, UIComponent component, Object value) {
        //Get the specific component ID
        String ID = component.getId();
        //Get the end time of the operating hours
        OperatingHours endTime = (OperatingHours) value;
        //Check the component ID
        if (ID.contains("Monday")) {
            String msg = validateOpHrs(component, endTime);
            //if message is not empty, display.
            if (!msg.isEmpty()) {
                FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, msg, msg);
                context.addMessage(component.getClientId(context), message);
            }
        } else if (ID.contains("Tuesday")) {
            String msg = validateOpHrs(component, endTime);
            //if message is not empty, display.
            if (!msg.isEmpty()) {
                FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, msg, msg);
                context.addMessage(component.getClientId(context), message);
            }
        } else if (ID.contains("Wednesday")) {
            String msg = validateOpHrs(component, endTime);
            //if message is not empty, display.
            if (!msg.isEmpty()) {
                FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, msg, msg);
                context.addMessage(component.getClientId(context), message);
            }
        } else if (ID.contains("Thursday")) {
            String msg = validateOpHrs(component, endTime);
            //if message is not empty, display.
            if (!msg.isEmpty()) {
                FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, msg, msg);
                context.addMessage(component.getClientId(context), message);
            }
        } else if (ID.contains("Friday")) {
            String msg = validateOpHrs(component, endTime);
            //if message is not empty, display.
            if (!msg.isEmpty()) {
                FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, msg, msg);
                context.addMessage(component.getClientId(context), message);
            }
        } else if (ID.contains("Saturday")) {
            String msg = validateOpHrs(component, endTime);
            //if message is not empty, display.
            if (!msg.isEmpty()) {
                FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, msg, msg);
                context.addMessage(component.getClientId(context), message);
            }
        } else if (ID.contains("Sunday")) {
            String msg = validateOpHrs(component, endTime);
            //if message is not empty, display.
            if (!msg.isEmpty()) {
                FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, msg, msg);
                context.addMessage(component.getClientId(context), message);
            }
        }
    }

    public String validateOpHrs(UIComponent component, OperatingHours endTime) {
        UIInput day = (UIInput) component.getAttributes().get("day");
        OperatingHours startTime = (OperatingHours) day.getValue();

        //If either start time of end time is off
        if (startTime.getLabel().equals("OFF") || endTime.getLabel().equals("OFF")) {
            //if confirm both is off
            if (startTime.getLabel().equals("OFF") && endTime.getLabel().equals("OFF")) {
                return "";
            } else {
                //set the component that value is deemed incorrect to false to disallow submitting of page
                ((UIInput) component).setValid(false);
                //return error message
                String statusMessage = systemMessages.getSystemMessage("OPERATING_HOURS_START_TIME_END_TIME_MUST_BE_OFF", preferredLocale);
                return statusMessage;
            }
        } else if (startTime.getTime().equals(endTime.getTime())) {
            //set the component that value is deemed incorrect to false to disallow submitting of page
            ((UIInput) component).setValid(false);
            //return error message
            String statusMessage = systemMessages.getSystemMessage("OPERATING_HOURS_START_TIME_END_TIME_MUST_BE_DIFFERENT", preferredLocale);
            return statusMessage;
        }
        return "";
    }

    public void validateEmail(FacesContext context, UIComponent toValidate, Object value) {
        String email = (String) value;
        Pattern p = Pattern.compile("^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$");

        //Match the given string with the pattern
        Matcher m = p.matcher(email);

        //Check whether match is found
        boolean matchFound = m.matches();

        //if mail does not match the pattern
        if (!matchFound) {
            ((UIInput) toValidate).setValid(false);
            String statusMessage = systemMessages.getSystemMessage("INVALID_EMAIL_ADDRESS", preferredLocale);
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, statusMessage, statusMessage);
            context.addMessage(toValidate.getClientId(context), message);
        }
    }
}
